<template>
  <a-table :columns="columns" :data-source="teacherList" bordered>
    <template #bodyCell="{ column, text, record }">
      <template v-if="['name', 'age', 'address'].includes(column.dataIndex)">
        <div>
          <a-input
              v-if="editableData[record.key]"
              v-model:value="editableData[record.key][column.dataIndex]"
              style="margin: -5px 0"
          />
          <template v-else>
            {{ text }}
          </template>
        </div>
      </template>
      <template v-else-if="column.dataIndex === 'operation'">
        <div class="editable-row-operations">
          <span v-if="editableData[record.key]">
            <a-typography-link @click="save(record.key)">Save</a-typography-link>
            <a-popconfirm title="Sure to cancel?" @confirm="cancel(record.key)">
              <a>Cancel</a>
            </a-popconfirm>
          </span>
          <span v-else>
            <a @click="edit(record.key)">Edit</a>
          </span>
        </div>
      </template>
    </template>
  </a-table>
</template>
<script setup lang="ts">
import { cloneDeep } from 'lodash-es';
import {defineComponent, onMounted, reactive, ref} from 'vue';
import type { UnwrapRef } from 'vue';
import myAxios from "../../plugins/myAxios";
import {message} from "ant-design-vue";
import type { Dayjs } from 'dayjs';
import dayjs from "dayjs";
const columns = [

  {
    title: '序号',
    dataIndex: 'id',
    width: '10%',
  },
  {
    title: '教师号',
    dataIndex: 'teacherNo',
    width: '10%',
  },
  {
    title: '老师姓名',
    dataIndex: 'teacherName',
    width: '10%',
  },
  {
    title: '性别',
    dataIndex: 'teacherGender',
    width: '10%',
  },
  {
    title: '家庭住址',
    dataIndex: 'teacherAddress',
    width: '10%',
  },
  {
    title: '出生日期',
    dataIndex: 'teacherBirthdate',
    width: '10%',
  },
  {
    title: '院系',
    dataIndex: 'teacherDeptname',
    width: '10%',
  },
  {
    title: '入职时间',
    dataIndex: 'arriveDate',
    width: '10%',
  },
  {
    title: '状态',
    dataIndex: 'teacherStatus',
    width: '5%',
  },
  {
    title: '备注',
    dataIndex: 'teacherRemarks',
    width: '10%',
  },


];
interface DataItem {
  key: string;
  name: string;
  age: number;
  address: string;
}
const data: DataItem[] = [];
for (let i = 0; i < 100; i++) {
  data.push({
    key: i.toString(),
    name: `Edrward ${i}`,
    age: 32,
    address: `London Park no. ${i}`,
  });
}

const teacherList = ref()

const dataSource = ref(teacherList);
const editableData: UnwrapRef<Record<string, DataItem>> = reactive({});

const edit = (key: string) => {
  editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
};
const save = (key: string) => {
  Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
  delete editableData[key];
};
const cancel = (key: string) => {
  delete editableData[key];
};


onMounted(()=>{
  getStudentList()
})
const getStudentList = async () =>{
  const res = await myAxios.get("/teacher/getAllTeacher")
  if (res.data){
    teacherList.value = res.data
    teacherList.value = teacherList.value.map(item=>{
      item.teacherStatus =item.teacherStatus===0?'离校':'在校'
      // item.createTime = dayjs(item.createTime).format('YYYY-MM-DD')
      return item
    })
    // console.log(studentDate.value)
  }else {
    message.error(res.message)
  }
}

</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>
